<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col
        :span="24"
        :xs="24"
      >
        <!-- 数据筛选表单 -->
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          class="form"
          label-width="100px"
        >
          <!-- 数据筛选表单组件 -->
          <el-form-item
            :label="$t('menumanage.menuname')"
            prop="menuName"
          >
            <el-input
              v-model="queryParams.menuName"
              :label="$t('menumanage.menunameplaceholder')"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item
            :label="$t('status')"
            prop="status"
          >
            <el-select
              v-model="queryParams.status"
              size="small"
              :placeholder="$t('menumanage.menustatusplaceholder')"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <!-- 数据筛选操作按钮 -->
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >
              {{ $t('query') }}
            </el-button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
            >
              {{ $t('reset') }}
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 数据操作按钮 -->
        <el-row :gutter="20">
          <el-col :span="3">
            <el-button
              type="primary"
              icon="el-icon-plus"
              style="margin-left:20px;width:130px;"
              @click="handleAdd"
            >
              {{ $t('menumanage.addmenu') }}
            </el-button>
          </el-col>
        </el-row>

        <!-- 数据表格 -->
        <el-table
          v-loading="loading"
          :data="menuList"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          default-expand-all
          :tree-props="{children: 'children'}"
        >
          <!-- 数据表格列 -->
          <el-table-column
            :label="$t('menumanage.menuname')"
            align="center"
            prop="menuName"
          />
          <el-table-column
            :label="$t('menumanage.menuid')"
            align="center"
            prop="id"
            width="100px"
          />
          <el-table-column
            :label="$t('menumanage.path')"
            align="center"
            prop="routePath"
          >
            <template v-slot:default="scope">
              {{ scope.row.routePath ? scope.row.routePath : '/' }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('menumanage.componet')"
            align="center"
            prop="componentPath"
          >
            <template v-slot:default="scope">
              {{ scope.row.componentPath ? scope.row.componentPath : '/' }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('menumanage.type')"
            :formatter="menuTypeFormat"
            align="center"
            prop="menuType"
            width="100px"
          >
            <template slot-scope="scope">
              <el-tag
                v-if="scope.row.menuType === 0"
                size="small"
              >
                {{ $t('menumanage.directory') }}
              </el-tag>
              <el-tag
                v-else-if="scope.row.menuType === 1"
                size="small"
                type="success"
              >
                {{ $t('menumanage.menu') }}
              </el-tag>
              <el-tag
                v-else-if="scope.row.menuType === 2"
                size="small"
              >
                {{ $t('menumanage.method') }}
              </el-tag>
            </template>
          </el-table-column>
          <!-- <el-table-column :label="$t('system_menu_menu_436')" align="center" prop="visible" width="100px" /> -->
          <el-table-column
            :label="$t('status')"
            align="center"
            prop="status"
            width="100px"
          >
            <template slot-scope="scope">
              <el-tag
                v-if="scope.row.status === 0"
                size="small"
                type="warning"
              >
                {{ $t('enabled') }}
              </el-tag>
              <el-tag
                v-else-if="scope.row.status === 1"
                size="small"
                type="danger"
              >
                {{ $t('disabled') }}
              </el-tag>
            </template>
          </el-table-column>
          <!-- 数据行操作按钮 -->
          <el-table-column
            :label="$t('operation')"
            align="center"
            class-name="small-padding fixed-width"
            width="200px"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >
                {{ $t('edit') }}
              </el-button>
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >
                {{ $t('delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 数据分页加载
        <el-pagination
          :current-page="pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="margin-left:350px;margin-top:5px;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @pagination="getList"
        /> -->
      </el-col>
    </el-row>
    <!-- 添加修改menu对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <!-- 数据编辑表单组件 -->
        <el-form-item
          :label="$t('menumanage.menuname')"
        >
          <el-tree
            ref="tree"
            :data="menuTree"
            :props="defaultProps"
            node-key="id"
            @node-click="handleNodeClick"
          />
        </el-form-item>
        <el-form-item
          :label="$t('menumanage.parentmenuid')"
          prop="parentId"
        >
          <el-input
            v-model="form.parentId"
            :placeholder="$t('menumanage.parentmenuidplaceholder')"
            disabled
          />
        </el-form-item>
        <el-form-item
          :label="$t('menumanage.menuname')"
          prop="menuName"
        >
          <el-input
            v-model="form.menuName"
            :placeholder="$t('menumanage.menunameplaceholder')"
          />
        </el-form-item>
        <el-form-item
          :label="$t('menumanage.type')"
          prop="menuType"
        >
          <el-select
            v-model="form.menuType"
            :placeholder="$t('menumanage.menutypeplaceholder')"
          >
            <el-option
              v-for="item in menuTypeOptions"
              :key="item.menuType"
              :label="item.label"
              :value="item.menuType"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('menumanage.path')"
          prop="path"
        >
          <el-input
            v-model="form.routePath"
            :placeholder="$t('menumanage.pathplaceholder')"
          />
        </el-form-item>
        <el-form-item
          :label="$t('menumanage.componet')"
          prop="component"
        >
          <el-input
            v-model="form.componentPath"
            :placeholder="$t('menumanage.componentplaceholder')"
          />
        </el-form-item>
      </el-form>
      <!-- 数据提交操作按钮 -->
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >
          {{ $t('submit') }}
        </el-button>
        <el-button @click="cancel">
          {{ $t('cancel') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/** 导入axios Api */
import { listMenu, treeMenu, getMenu, addMenu, updateMenu, deleteMenu } from '@/api/system/menu'
  import { mapGetters } from 'vuex';
  import {EventBus} from '@/eventbus/EventBus'
export default {
  name: 'Menu',
  data() {
    return {
              lang:null,
        languageparams:null,
      // 状态类型
      statusOptions: [
        {
          value: 0,
          label: this.$t('menumanage.enabled')
        },
        {
          value: 1,
          label: this.$t('menumanage.disabled')
        }
      ],
      // 遮罩层
      loading: false,
      // Menu表格数据
      menuList: [],
      // 对话框目录+菜单树
      menuTree: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 数据查询参数
      queryParams: {
        menuName: null,
        status: null
      },
      // 数据编辑表单
      form: {},
      // 表单数据校验规则
      rules: {
        menuName: [
          { required: true, message: this.$t('menumanage.menunamerules'), trigger: 'blur' }
        ],
        menuType: [
          { required: true, message: this.$t('menumanage.menutyperules'), trigger: 'blur' }
        ]
      },
      menuTypeOptions: [{
        menuType: 0,
        label: this.$t('menumanage.directory')
      },
      {
        menuType: 1,
        label: this.$t('menumanage.menu')
      },
      {
        menuType: 2,
        label: this.$t('menumanage.method')
      }],
      defaultProps: {
        children: 'children',
        id: 'id',
        label: 'menuName'
      }
    }
  },
  computed: {
    ...mapGetters(["language"]),
  },
  watch:{

  },
  created() {
    this.getList()
  },
    beforeDestroy() {
      EventBus.$off('language-changed')
    },
    mounted() {
      EventBus.$on('language-changed', this.handleLanguageChange);
    },
  methods: {
    initLanguage(lang){
      // console.log(lang)
      if(lang=='en_US'){
        this.languageparams = 'en'
      }
      else if (lang === 'zh_CNS') {
        this.languageparams = null;
      }
    },
    handleLanguageChange(lang) {
      this.lang = lang; // 更新组件的lang数据
        if (this.lang === 'en_US') {
        this.languageparams = 'en';
      } else if (this.lang === 'zh_CNS') {
        this.languageparams = null;
      }
      this.getList();
      this.getTree();
    },
    /** 查询Menu数据列表 */
    getList() {
      this.loading = true
      console.log(this.language)
      // this.initLanguage(this.language)
      // console.log("getList this.languageparams: ")
      console.log(this.languageparams)
      listMenu(this.queryParams,this.languageparams).then((response) => {
        // console.log(response.data)
        this.menuList = response.data
        this.loading = false
      })
    },
    /** 得到对话框父级菜单树 */
    getTree(){
      this.loading = true
      // this.initLanguage(this.language)
      // console.log("getTree this.languageparams: ")
      console.log(this.languageparams)
      treeMenu(this.languageparams).then((response) => {
        this.menuTree = response.data
        this.loading = false
      })
    },
    /** table菜单类型格式化 */
    menuTypeFormat(row) {
      let type = row.menuType
      switch (type) {
        case 0:
          type = this.$t('menumanage.menu')
          break
        case 1:
          type = this.$t('menumanage.menu')
          break
        case 2:
          type = this.$t('menumanage.button')
          break
      }
      return type
    },
       /** 数据编辑对话框取消按钮 */
    cancel() {
      this.open = false
      this.resetForm()
    },

    /** 数据编辑表单清空 */
    resetForm() {
      this.form = {
        menuId: null,
        menuName: null,
        path: null,
        component: null,
        parentId: 0,
        menuType: null,
        status: null
      }
      // 得到父级菜单树
      this.getTree()
    },

    /** 数据筛选搜索按钮 */
    handleQuery() {
      this.getList()
      this.resetQueryParams()
      this.$message.success(this.$t('querysuccess'))
    },

    /** 数据筛选重置按钮 */
    resetQuery() {
      this.queryParams = {
        menuName: null,
        status: null
      }
      this.getList()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm()
      this.open = true
      this.title = this.$t('menumanage.addTitle')
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm()
      getMenu(Number(row.id)).then((response) => {
        this.form = response.data
        this.open = true
        this.title = this.$t('menumanage.editTitle')
      })
    },

    /** 数据提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id === null) {
            addMenu(this.form).then((response) => {
              this.$message.success(this.$t('addsuccess'))
              this.open = false
              this.title = ''
              this.getList()
            })
          } else {
            updateMenu(this.form).then((response) => {
              this.$message.success(this.$t('editsuccess'))
              this.open = false
              this.title = ''
              this.getList()
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        this.$t('deletewarning1') + row.menuName + '"', this.$t('warning'), {
          confirmButtonText: this.$t('yes'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        })
        .then(function() {
          return deleteMenu(Number(row.id))
        })
        .then(() => {
          this.getList()
          this.$message.success(this.$t('deletesuccess'))
        })
        .catch(() => {})
    },
    /** 选中上级菜单 */
    handleNodeClick(data) {
        this.form.parentId = data.id
    }
  }
}
</script>
<style lang="scss" scoped>
.form {
  border: 2px dashed rgb(211, 211, 211);
  padding: 20px;
  padding-bottom: 0px;
  margin: 10px 20px 20px 20px;
}
</style>
